package cn.mayaslight.common.http;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.RequestBuilder;
import cn.mayaslight.common.exception.HandleException;
import cn.mayaslight.common.handler.JsonResponseHandler;
import cn.mayaslight.common.handler.XmlResponseHandler;
import cn.mayaslight.common.param.IParamIn;
import cn.mayaslight.common.pojo.HttpAPIEntity;
import cn.mayaslight.common.springbean.APICaller;
import cn.mayaslight.common.springbean.HttpAPIGetter;

public class RequestBuilderPack<T> {

	private RequestBuilder requestBuilder;
	private ResponseHandler<T> responseHandler;
	private int type;

	private RequestBuilderPack() {

	}

	public static <T> RequestBuilderPack<T> create(IParamIn param, HttpAPIGetter apiGetter) {
		return create(param, apiGetter, null);
	}

	public static <T> RequestBuilderPack<T> create(IParamIn param, HttpAPIGetter apiGetter, Class<T> targetClass) {
		RequestBuilderPack<T> requestBuilder = new RequestBuilderPack<T>();
		String apiKey = param.getApiKey();
		if (StringUtils.isBlank(apiKey)) {
			throw new HandleException("ApiKey为空!");
		}
		try {
			StringBuilder sb = new StringBuilder(apiKey);
			boolean result = new HttpMethodJudgement<T>(requestBuilder, sb).setTheNextChain(new HttpDataTypeJudgement<T>(requestBuilder, sb)).exec();
			if(result){
				requestBuilder.getRequestBuilder().setUri(sb.toString());
			}else{
				HttpAPIEntity apiEntity = apiGetter.getAPI(param.getApiKey());
				String uri = apiEntity.getApiUrl();
				if (APICaller.POST == apiEntity.getApiMethod()) {
					requestBuilder.setRequestBuilder(RequestBuilder.post());
				} else if (APICaller.GET == apiEntity.getApiMethod()) {
					requestBuilder.setRequestBuilder(RequestBuilder.get());
				} else {
					throw new HandleException("ApiKey对应的连接不为GET或者POST!");
				}
				if(APICaller.JSON != apiEntity.getApiDataType() && APICaller.XML != apiEntity.getApiDataType()){
					throw new HandleException("数据类型必须为JSON或者XML!");
				}
				requestBuilder.setType(apiEntity.getApiDataType());
				requestBuilder.getRequestBuilder().setUri(uri);
			}
			return requestBuilder.setResponseHandler(targetClass);
		} catch (Exception e) {
			throw new HandleException(e);
		}
	}

	public ResponseHandler<T> getResponseHandler() {
		return responseHandler;
	}

	public RequestBuilderPack<T> setResponseHandler(Class<T> targetClass) {
		if (targetClass == null) {
			throw new HandleException("传入TargetClass为null!");
		}
		if (APICaller.JSON == type) {
			responseHandler = JsonResponseHandler.createResponseHandler(targetClass);
		} else if (APICaller.XML == type) {
			responseHandler = XmlResponseHandler.createResponseHandler(targetClass);
		} else {
			throw new HandleException("DateType必须为JSON或者XML!");
		}
		return this;
	}

	public RequestBuilder getRequestBuilder() {
		return requestBuilder;
	}

	public void setRequestBuilder(RequestBuilder requestBuilder) {
		this.requestBuilder = requestBuilder;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
